//
//  UIView+RAExtension.m
//  RayoSmartBleKey
//
//  Created by Piccolo on 2017/12/1.
//  Copyright © 2017年 Piccolo. All rights reserved.
//

#import "UIView+RAExtension.h"

@implementation UIView (RAExtension)

+ (instancetype)createViewFromNibName:(NSString *)nibName
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
    return [nib objectAtIndex:0];
}

+ (instancetype)createViewFromNib
{
    return [self createViewFromNibName:NSStringFromClass(self.class)];
}

- (void)ra_setWidth:(CGFloat)width {
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), width, CGRectGetHeight(self.frame));
}

- (void)ra_setHeight:(CGFloat)height {
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), height);
}

- (void)ra_setOriginX:(CGFloat)x {
    self.frame = CGRectMake(x, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}

- (void)ra_setOriginY:(CGFloat)y {
    self.frame = CGRectMake(CGRectGetMinX(self.frame), y, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}

- (void)ra_setRightOriginX:(CGFloat)x {
    if (!self.superview) {
        return;
    } else {
        UIView *superView = self.superview;
        CGRect superViewFrame = superView.frame;
        /*
         修复站点列表选择的时候,距离会显示不全的 问题
         */
        if (![RAUtil checkPlatformIsSixType] && CGRectGetWidth(superView.frame) == IPHONE_6_SCREEN_WIDTH) {
            superViewFrame.size.width *= SCREEN_WIDTH_COEFFIENT;
            self.frame = CGRectMake((CGRectGetWidth(superViewFrame) - CGRectGetWidth(self.frame) - x) / SCREEN_WIDTH_COEFFIENT, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame) / SCREEN_WIDTH_COEFFIENT, CGRectGetHeight(self.frame));
        } else {
            self.frame = CGRectMake(CGRectGetWidth(superViewFrame) - CGRectGetWidth(self.frame) - x, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        }
    }
}

- (void)ra_resetWithDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY deltaWidth:(CGFloat)deltaWidth deltaHeight:(CGFloat)deltaHeight {
    CGRect viewFrame = self.frame;
    viewFrame = CGRectMake(CGRectGetMinX(viewFrame) + deltaX, CGRectGetMinY(viewFrame) + deltaY, CGRectGetWidth(viewFrame) + deltaWidth, CGRectGetHeight(viewFrame) + deltaHeight);
    self.frame = viewFrame;
}

- (void)ra_setBorderColor:(UIColor *)color width:(CGFloat)width {
    
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
}

@end
